// 
// Created By: haolong 2018/05/26
// 

#include "cybertron/core/SimOneServerInfo.hpp"
#include "cybertron/core/ConfigureClient.hpp"
#include "cybertron/core/UtilString.hpp"
#include "cybertron/core/SimOneEnv.hpp"
#include <atomic>
#include <iostream>

CYBERTRON_BEGIN
SimOneServerInfo::SimOneServerInfo() {
	mSonServerInfo.serverAddr = "127.0.0.1";
	mSonServerInfo.port = 23790;

	mLogServerInfo.serverAddr = "127.0.0.1";
	mLogServerInfo.port = 9999;

	mAlarmServerInfo.serverAddr = "127.0.0.1";
	mAlarmServerInfo.port = 2200;

	mDevopsServerInfo.serverAddr = "127.0.0.1";
	mDevopsServerInfo.port = 8080;
}

SimOneServerInfo::~SimOneServerInfo() {

}

CybertronCoreAPI  bool SimOneServerInfo::Init() {
	std::string value;
	std::string error;
	bool ret = false;

	//SimOneEnv::GetInstance().Init();
	if (SimOneEnv::GetInstance().IsCloudEnv())
	{
		createConfigureClient();
		ret = cccGetValueFunction("common", "SONServerInfo", value, error);
		if (ret) {
			std::vector<std::string> result;
			UtilString::split(result, value, ';');
			if (result.size() == 2) {
				mSonServerInfo.serverAddr = result[0];
				mSonServerInfo.port = std::atoi(result[1].c_str());
			}
		}
		else
		{
			return false;
		}
		ret = cccGetValueFunction("common", "LogServerInfo", value, error);
		if (ret) {
			std::vector<std::string> result;
			UtilString::split(result, value, ';');
			if (result.size() == 2) {
				mLogServerInfo.serverAddr = result[0];
				mLogServerInfo.port = std::atoi(result[1].c_str());
			}
		}
		ret = cccGetValueFunction("common", "AlarmServerInfo", value, error);
		if (ret) {
			std::vector<std::string> result;
			UtilString::split(result, value, ';');
			if (result.size() == 2) {
				mAlarmServerInfo.serverAddr = result[0];
				mAlarmServerInfo.port = std::atoi(result[1].c_str());
			}
		}
		ret = cccGetValueFunction("common", "DevopsServerInfo", value, error);
		if (ret) {
			std::vector<std::string> result;
			UtilString::split(result, value, ';');
			if (result.size() == 2) {
				mDevopsServerInfo.serverAddr = result[0];
				mDevopsServerInfo.port = std::atoi(result[1].c_str());
			}
		}
		return ret;
	}

	return ret;
}

CybertronCoreAPI  bool SimOneServerInfo::getSONServerInfo(SONServerInfo* pServerInfo) {
	pServerInfo->serverAddr = mSonServerInfo.serverAddr;
	pServerInfo->port = mSonServerInfo.port;
	return true;
}

CybertronCoreAPI  bool SimOneServerInfo::getLogServerInfo(LogServerInfo* pServerInfo) {
	pServerInfo->serverAddr = mLogServerInfo.serverAddr;
	pServerInfo->port = mLogServerInfo.port;
	return true;
}

CybertronCoreAPI  bool SimOneServerInfo::getAlarmServerInfo(AlarmServerInfo* pServerInfo) {
	pServerInfo->serverAddr = mAlarmServerInfo.serverAddr;
	pServerInfo->port = mAlarmServerInfo.port;
	return true;
}

CybertronCoreAPI  bool SimOneServerInfo::getDevOpsServerInfo(DevOpsServerInfo* pServerInfo) {
	pServerInfo->serverAddr = mDevopsServerInfo.serverAddr;
	pServerInfo->port = mDevopsServerInfo.port;
	return true;
}
CYBERTRON_END
